package com.lt.controller.teacher;

import com.lt.dto.ClosingAuditDto;
import com.lt.dto.MyProject;
import com.lt.dto.ProgressRecord;
import com.lt.dto.StudentApplyDto;
import com.lt.dto.teacher.ClosingReview;
import com.lt.dto.teacher.InterimReview;
import com.lt.dto.teacher.ProjectApproval;
import com.lt.entity.Notice;
import com.lt.entity.Student;
import com.lt.entity.Teacher;
import com.lt.service.NoticeService;
import com.lt.service.ProjectManageService;
import com.lt.service.ProjectService;
import com.lt.service.StuAndTeacherService;
import com.lt.utils.JwtUtils;
import com.lt.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @Author :liteng
 * @Time : 2023/11/4 17:19
 */

@RestController
@Slf4j
@RequestMapping("/teacher")
@CrossOrigin
public class TeacherController {

    private final StuAndTeacherService stuAndTeacherService;

    private final NoticeService noticeService;

    private final ProjectService projectService;
    private final ProjectManageService projectManageService;


    public TeacherController(StuAndTeacherService stuAndTeacherService, NoticeService noticeService, ProjectService projectService, ProjectManageService projectManageService) {
        this.stuAndTeacherService = stuAndTeacherService;
        this.noticeService = noticeService;
        this.projectService = projectService;
        this.projectManageService = projectManageService;
    }

    /**
     * 根据教师号查询老师(老师)
     *
     * @param teacherNum
     * @return
     */
    @GetMapping("/num/{teacherNum}")
    public Result findTeacher(@PathVariable("teacherNum") Long teacherNum) {
        Teacher teacher = stuAndTeacherService.findByTeacherNum(teacherNum);
        if (teacher == null) {
            return Result.error("教师号输入错误，未查询到教师");
        }
        return Result.success(teacher);
    }

    /**
     * 根据项目编号获取项目负责人（学生）
     *
     * @param projectId
     * @return
     */
    @GetMapping("/chargeStu")
    public Result getChargeStuByProject(Long projectId) {
        Student chargeStu = projectService.getChargeStu(projectId);
        return Result.success(chargeStu);
    }
///////////////////////////////////////////////////

    /**
     * 教师获取记录
     * 项目申报
     *
     * @return
     */
    @GetMapping("/establish")
    public Result getProject() {
        List<MyProject> allProject = projectService.getAllProject();
        return Result.success(allProject);
    }

    /**
     * 教师通过项目
     * 项目申报
     *
     * @param projectApproval
     * @return
     */
    @PostMapping("/establish/status")
    public Result scoreProject(@RequestBody ProjectApproval projectApproval) {
        log.error(String.valueOf(projectApproval));
        if (projectManageService.setStatusAndScore(projectApproval))
            return Result.success();
        else
            return Result.error("状态修改失败");
    }
    /////////////////////////////////////////////////////////////////////////////////////

    /**
     * 教师获取项目
     * 中期审核
     *
     * @return
     */
    @GetMapping("/interim")
    public Result getInterimReview() {
        List<ProgressRecord> records = projectService.getRecords();
        return Result.success(records);
    }

    /**
     * 中期审核
     * 教师获取参加项目人员
     *
     * @param projectId
     * @return
     */
    @GetMapping("/interim/members")
    public Result getMembers(Long projectId) {
        List<StudentApplyDto> joinedStudents = projectService.getJoinedStudents(projectId);

        return Result.success(joinedStudents);
    }

    /**
     * 教师通过项目
     * 中期审核
     *
     * @param interimReview
     * @return
     */
    @PostMapping("/interim/status")
    public Result InterimReview(@RequestBody InterimReview interimReview) {
        if (projectManageService.setInterimStatus(interimReview))
            return Result.success();
        return Result.error("修改失败");
    }

    ////////////////////////////////////////////////////////////////////////////////

    /**
     * 教师通过项目
     * 结项审核
     *
     * @return
     */
    @GetMapping("/closingAudit")
    public Result closingAudit() {
        List<ClosingAuditDto> endList = projectService.getEndList();
        return Result.success(endList);

    }

//    /**
//     * 获取项目负责人
//     *
//     * @param projectId
//     * @return
//     */
//    public Result getDirector(Long projectId) {
//
//        return null;
//    }

    /**
     * 教师端
     * 结项审核：更改状态
     *
     * @return
     */
    @PostMapping("/closingAudit/status")
    public Result closingAuditStatus(@RequestBody ClosingReview closingReview) {
        if (projectManageService.setClosingAuditStatus(closingReview))
            return Result.success();
        return Result.error("修改失败");
//        return null;

    }

    ////////////////////////////////////////////////////////////////////////////////

    /**
     * 教师端
     * 展示所有公告
     *
     * @return
     */
    @GetMapping("/noticeList")
    public Result showNotice(int start, int pageSize, String key, int dept) {
        List<Notice> notices = noticeService.getNotices(key, dept, start, pageSize);
        return Result.success(notices);
    }

    /**
     * 教师修改公告,获取之前的内容
     *
     * @return
     */
    @GetMapping("/modifyNotice")
    public Result getToBeModifiedOne(Long noticeId) {
        Notice toBeModifiedOne = noticeService.getToBeModifiedOne(noticeId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("title", toBeModifiedOne.getTitle());
        map.put("content", toBeModifiedOne.getContent());

        return Result.success(map);
    }

    /**
     * 教师修改公告
     *
     * @return
     */
    @Transactional
    @PostMapping("/modifyNotice")
    public Result alterNotice(@RequestHeader(value = "token") String jwt,
                              @RequestBody Notice notice) {
        //获取当前用户号
        Long num;
        try {
            num = JwtUtils.getNumFromJwt(jwt);
        } catch (Exception e) {
            return Result.error("用户验证错误");
        }
        if (noticeService.updateNotice(notice, num)) {
            return Result.success("修改成功");
        } else {
            return Result.error("修改失败");
        }
//        return Result.success();
    }

    ///////////////////////////////////////////////////////////////////
}
